package com.liuguang.realgateway.utils;

import com.alibaba.fastjson.JSON;
import com.liuguang.common.pojo.CommonResult;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * @ClassName: GatewayUtil
 * @Description：
 * @Author: wanghr
 * Date: 2:14 PM 2020/10/29
 * Version: 1.0
 */
public class GatewayUtil {
    /**
     * 返回前端友好提示
     */
    public static Mono<Void> feedbackFriendly(ServerHttpResponse response, String msg) {
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        response.setStatusCode(HttpStatus.OK);
        return response.writeWith(Mono.fromSupplier(() -> {
            DataBufferFactory dataBufferFactory = response.bufferFactory();
            byte[] bytes = JSON.toJSONBytes(CommonResult.fail(msg));
            return dataBufferFactory.wrap(bytes);
        }));
    }
}
